var ctxPath = "/education";

/**
 * Javascript API调用ActionSheet
 */
!function ($) {
    var $myAs = $('#J_ActionSheet');

    $('#J_ShowActionSheet').on('click', function () {
        $myAs.actionSheet('open');
        $(".beSure").remove();
        $myAs.append("<div id=\"createOrderBtn\" class=\"beSure\">确定</div>");
    });

    $("#shoppingCart").on('click', function () {
        if($("#shoppingCart").hasClass('add-active')){
            var request = {};
            request.productId = $.getUrlParam('id');
            request.productType = 0;

            $.ajax({
                url: ctxPath + '/wx/api/customer/shoppingCart',
                contentType: "application/json",
                type: 'post',
                data: JSON.stringify(request),
                success: function (result) {
                    if(result.returnCode == 0) {
                        $("#shoppingCart").removeClass("add-active");
                    } else {
                        alert(result.returnMsg);
                    }
                }
            });
        }else{
            $myAs.actionSheet('open');
            $(".beSure").remove();
            $myAs.append("<div id=\"addShopingCart\" class=\"beSure\">加入购物车</div>");
        }
    });

    $("#collect").on('click', function () {
        var productId = $.getUrlParam('id');

        $.ajax({
            url: ctxPath + '/wx/api/customer/collect',
            type: 'post',
            dataType: 'json',
            data: {productId: productId, productType: 0},
            success: function (result) {
                if(result.returnCode == 0) {
                    if($("#collect").hasClass('add-active')){
                        $("#collect").removeClass("add-active");
                    }else{
                        $("#collect").addClass('add-active');
                    }
                } else if(result.returnCode == -2){
                    alert(result.returnMsg);
                    window.location.href = "/education/wx/customerLogin.html?redirect=/education/wx/studyToursProductDetail.html?id=" + productId;
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    })

    $('#J_Cancel').on('click', function () {
        $myAs.actionSheet('close');
    });



}(jQuery);

!function () {

    var $target = $('#J_School_Grade_Clazz');

    $target.on('click', function (event) {
        event.stopPropagation();
        $target.citySelect('open');
    });

    $target.on('done.ydui.cityselect', function (ret) {
        $(this).val(ret.provance + ',' + ret.city + ',' + ret.area);
    });
}();


/**
 * Javascript API调用ActionSheet
 */
!function ($) {
    var detailId = $.getUrlParam('id');

    /**
     * 初始化游学产品详情方法
     * @param id
     */
    var initProductDetail = function(id) {
        $.ajax({
            url: ctxPath + '/wx/api/product/getStudyToursProductById',
            type: 'post',
            dataType: 'json',
            data: {studyToursProductId: id},
            success: function (result) {
                if(result.returnCode == 0) {
                    var data = result.studyToursProductPo;

                    if (data != null) {
                        $("input[name='productCode']").val(data.code);
                        $("input[name='productDeposit']").val(data.deposit);

                        $(".showImage").attr("src", data.showImage);
                        $("#buyShowImage").attr("src", data.showImage);
                        $("#deposit-and-price").html("¥" + data.deposit + "/¥" + data.price);
                        $("#productName").html(data.name);

                        var country = "";
                        if("RUSSIA" == data.country){
                            country = "俄罗斯";
                        }else if("BRITAIN" == data.country){
                            country = "英国";
                        }else if("GERMANY" == data.country){
                            country = "德国";
                        }else if("EUROPE" == data.country){
                            country = "欧洲"
                        }

                        $("#productCountry").html(country);
                        $("#productDetail").html(data.detail);

                        var schedules = data.schedule.split(";");
                        $.each(schedules, function(index, ele){
                            if("" != ele){
                                if(index == 0){
                                    $("#productSchedule").append("<li th:class='red'>" +
                                        "<h3>" + ele.split(",")[0] + "</h3>" +
                                        "<dl><dt>" + ele.split(",")[1] + "</dt></dl>" +
                                        "</li>");
                                }else{
                                    $("#productSchedule").append("<li>" +
                                        "<h3>" + ele.split(",")[0] + "</h3>" +
                                        "<dl><dt>" + ele.split(",")[1] + "</dt></dl>" +
                                        "</li>");
                                }
                            }
                        });

                        $("#buyProductName").html(data.name);
                        $("#productDeposit").html("¥" + data.deposit + "/¥" + data.price);

                        if(result.isCollected == 1){
                            $("#collect").addClass("add-active");
                        }

                        if(result.isShoppingCart == 1){
                            $("#shoppingCart").addClass("add-active");
                        }
                    }
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    };

    initProductDetail(detailId);

    $("body").delegate("#createOrderBtn","click", function(){
        var request = {};

        request.productImage = $("#buyShowImage").attr("src");
        request.productName = $("#productName").html();
        request.productCode = $("input[name='productCode']").val();
        request.productDeposit = $("input[name='productDeposit']").val();
        request.applicantName = $("input[name='applicantName']").val();
        request.groupName = $("input[name='groupName']").val();
        request.phone = $("input[name='phone']").val();
        request.schoolGradeClazz = $("#J_School_Grade_Clazz").val();
        request.recommenderCode = $("input[name='recommenderCode']").val();
        request.openid = $.cookie('wxopenid');
        request.productType = 0;

        if(null == request.applicantName || "" == request.applicantName){
            alert("请输入报名人姓名！");
            return;
        }

        if(null == request.groupName || "" == request.groupName){
            alert("请输入参团人姓名！");
            return;
        }

        if(null == request.phone || "" == request.phone || !(/^1[3|4|5|6|7|8|9]\d{5,9}$/.test(request.phone))){
            alert("请输入正确的手机号！");
            return;
        }

        if(null == request.schoolGradeClazz || "" == request.schoolGradeClazz){
            alert("请选择学校年级班级！");
            return;
        }

        //创建订单，跳转至支付页面
        $.ajax({
            url: ctxPath + '/wx/api/order/createOrder',
            contentType: "application/json",
            type: 'post',
            data: JSON.stringify(request),
            success: function (result) {
                if(result.returnCode == 0) {
                    window.location.href = ctxPath + "/wx/productPay.html?orderCode=" + result.orderCode;
                } else {
                    alert(result.returnMsg);
                }
            }
        });
    });

    $("body").delegate("#addShopingCart","click", function(){
        var request = {};

        request.productId = $.getUrlParam('id');
        request.productImage = $("#buyShowImage").attr("src");
        request.productName = $("#productName").html();
        request.productCode = $("input[name='productCode']").val();
        request.productDeposit = $("input[name='productDeposit']").val();
        request.applicantName = $("input[name='applicantName']").val();
        request.groupName = $("input[name='groupName']").val();
        request.phone = $("input[name='phone']").val();
        request.schoolGradeClazz = $("#J_School_Grade_Clazz").val();
        request.recommenderCode = $("input[name='recommenderCode']").val();
        request.openid = $.cookie('wxopenid');
        request.productType = 0;

        if(null == request.applicantName || "" == request.applicantName){
            alert("请输入报名人姓名！");
            return;
        }

        if(null == request.groupName || "" == request.groupName){
            alert("请输入参团人姓名！");
            return;
        }

        if(null == request.phone || "" == request.phone || !(/^1[3|4|5|6|7|8|9]\d{5,9}$/.test(request.phone))){
            alert("请输入正确的手机号！");
            return;
        }

        if(null == request.schoolGradeClazz || "" == request.schoolGradeClazz){
            alert("请选择学校年级班级！");
            return;
        }

        $.ajax({
            url: ctxPath + '/wx/api/customer/shoppingCart',
            contentType: "application/json",
            type: 'post',
            data: JSON.stringify(request),
            success: function (result) {
                if(result.returnCode == 0) {
                    alert("加入购物车成功！");
                    $('#J_ActionSheet').actionSheet('close');
                    $("#shoppingCart").addClass("add-active");
                } else {
                    alert(result.returnMsg);
                    if(result.returnMsg=="请先登录！") {
                        window.location.href = "/education/wx/customerLogin.html?redirect=/education/wx/studyToursProductDetail.html?id=" + detailId;
                    }
                }
            }
        });
    });
}(jQuery);